<?php

class BannerHelper{
    
    public function randomTop(){
        return BannerHelper::random(1);
    }
    
    public function randomMiddle(){
        return BannerHelper::random(2);
    }
    
    public function random($pos = 1){
        $dir = Config::get('public_path').'/banners/'.PaisHelper::pais().'/'.$pos;
        if(is_dir($dir)){
            $files = glob($dir.'/*.html');
            if(!empty($files)){
              $file = $files[rand(0, count($files)-1)];
              echo str_replace("{BASE_URL}",HTMLHelper::baseUrl(1),file_get_contents($file));
            } else {
              return BannerHelper::randomDefault($pos);
            }
        }else{
            return BannerHelper::randomDefault($pos);
        }
    }
    
    public function randomDefault($pos = 1){
        $dir = Config::get('public_path').'/banners/default/'.$pos;
        if(is_dir($dir)){
            $files = glob($dir.'/*.html');
            $file = $files[rand(0, count($files)-1)];
            echo str_replace("{BASE_URL}",HTMLHelper::baseUrl(1),file_get_contents($file));
        }
    }
    
}